/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.wwm.DAO.impl;

import edu.fjut.se1603.wwm.DAO.AccountDao;
import edu.fjut.se1603.wwm.DAO.BaseDao;
import edu.fjut.se1603.wwm.entity.Account;
import edu.fjut.se1603.wwm.entity.Pet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daydream
 */
public class AccountDaoImpl extends BaseDao implements AccountDao{

    @Override
    public int add(Account account) {
        int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst=con.prepareStatement("insert into account(DEAL_TYPE,PET_ID,SELLER_ID,BUYER_ID,PRICE,DATE_TIME) values(?,?,?,?,?,?)");
            pst.setInt(1, account.getDeal_type());
            pst.setInt(2, account.getPet_id());
            pst.setInt(3, account.getSeller_id());
            pst.setInt(4,account.getBuyer_id());
            pst.setInt(5, account.getPrice());
            pst.setDate(6, account.getDate_time());
            r=pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AccountDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public Account getById(int id) {
        Account account=new Account();
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst =con.prepareStatement("select * from account where ID=?");
            pst.setInt(1, id);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                account.setDeal_type(rs.getInt("DEAL_TYPE"));
                account.setPet_id(rs.getInt("PET_ID"));
                account.setSeller_id(rs.getInt("SELLER_ID"));
                account.setBuyer_id(rs.getInt("BUYER_ID"));
                account.setPrice(rs.getInt("PRICE"));
                account.setDate_time(rs.getDate("DATE_TIME"));
            }
            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(AccountDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return account;
    }

    @Override
    public List<Account> getList() {
        List<Account> accounts=new ArrayList<Account>();
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst=con.prepareStatement("select * from account");
            ResultSet rs=pst.executeQuery();
            while(rs.next())
            {
                Account account =new Account();
                account.setDeal_type(rs.getInt("DEAL_TYPE"));
                account.setPet_id(rs.getInt("PET_ID"));
                account.setSeller_id(rs.getInt("SELLER_ID"));
                account.setBuyer_id(rs.getInt("BUYER_ID"));
                account.setPrice(rs.getInt("PRICE"));
                account.setDate_time(rs.getDate("DATE_TIME"));
                accounts.add(account);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AccountDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return accounts;
    }

    @Override
    public int delete(Integer id) {
        int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst=con.prepareStatement("delete from account where ID="+id);
            r=pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(AccountDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public int change(Account account) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
